<?php

class Pipeline {

	protected $pipe = [];
	protected $payload;

	public function __construct($payload)
	{
		$this->payload = $payload;
	}

	public function pipe($stage)
	{
		$this->pipe[] = $stage;
		return $this;
	}

	public function then()
	{
		// foreach($this->pipe as $stage) {
		// 	$this->payload = $stage->handler($this->payload);
		// }

		array_reduce(array_reverse($this->pipe), function ($init, $stage) {

			is_object($stage) && $this->payload = call_user_func([$stage, 'handler'], $this->payload);

		}, null);

		return $this->payload;
	}


}


?>